package com.epro.dx.task;

import java.util.ArrayList;
import java.util.List;

import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.OnekeyFilterLocalUtil;
import com.weipu.common.constants.Constant;
import com.weipu.common.constants.ServerConstant;
import com.weipu.common.facade.content.api.CategoryService;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.impl.ProductServiceProviderImpl;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.OneKeyFilterResponseModel;
import com.weipu.common.util.JsonFormat;
import com.weipu.common.util.Logger;
import com.weipu.common.util.StringUtil;

import android.content.Context;
import android.os.AsyncTask;

/************************
 * @author Tony
 * @Date:2016-4-6下午1:31:06
 * @描述:获取用户一级分类的任务
 ************************/
public class GetTopCategoryAsyncTask extends AsyncTask<Void, Void, String> {

    private MyAsyncTaskListenner myAsyncTaskListenner;
    private Context mContext;
    private ArrayList<CatelogModel> topCategories;
    private CategoryService categoryService = ProviderFactory.createCategoryServiceDBProvider();

    public GetTopCategoryAsyncTask(Context mContext, MyAsyncTaskListenner myAsyncTaskListenner) {
        this.mContext = mContext;
        this.myAsyncTaskListenner = myAsyncTaskListenner;
    }

    @Override
    protected void onPreExecute() {
        if (null != myAsyncTaskListenner) {
            myAsyncTaskListenner.onPreTask();
        }
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            topCategories = ProviderFactory.createProductProvider().getTopCategories(ActivityUtil.getCustomerID(),
                    OnekeyFilterLocalUtil.getFilterState(mContext), OnekeyFilterLocalUtil.getLocalCategoryIds(mContext));

            if (null != topCategories && topCategories.size() > 0) {
                //将数据更新到本地数据库
                refreshLocalData(topCategories);
                //记录缓存时间
                StringUtil
                        .saveConfig(
                                ServerConstant.RequestMethodNameConstant.METHOD_GETTOPCATEGORIES,
                                System.currentTimeMillis() + "");
            }
        } catch (DxException e) {
            return e.getErrCode();
        }
        return "0";
    }

    @Override
    protected void onPostExecute(String result) {
        if (null != myAsyncTaskListenner) {
            myAsyncTaskListenner.onPostTask(result, topCategories);
        }
    }

    /**
     * @param topCategories
     * @创建：Tony 2016-4-6下午1:55:33
     * @描述：刷新本地数据
     */
    private void refreshLocalData(ArrayList<CatelogModel> topCategories) {
        if (topCategories == null || topCategories.size() <= 0) {
            return;
        }
        //查询本地存储
        ArrayList<CatelogModel> oldCateoryList = categoryService.queryCategoryListByParentCategoryID(
                ProductServiceProviderImpl.TOP_CATEGORY_DEFAULT_PARENT_ID);
        if (null != oldCateoryList && oldCateoryList.size() > 0) {
            //清空老数据
            categoryService.deleteCategoryListByParentID(ProductServiceProviderImpl.TOP_CATEGORY_DEFAULT_PARENT_ID);
        }
        //将新数据保存到数据库
        for (int i = 0; i < topCategories.size(); i++) {
            categoryService.saveCategoryInfo(topCategories.get(i));
        }
    }

}
